﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using svCore.App;
using DemoAntiPhishing.Properties;
using Vux.Helper.File;

namespace svCore
{
    public partial class frmProfile : Form
    {
        private UserProfile m_user_profile;

        public UserProfile UserProfile
        {
            get { return m_user_profile; }
            set { m_user_profile = value; }
        }

        public frmProfile()
        {
            InitializeComponent();
        }

        private void frmProfile_Load(object sender, EventArgs e)
        {
            try
            {
                this.profileImageBox.Image = LoadImageProfile();
                this.label2.Text = "No file chosen";

                // chỉ được enable khi nút choose được click
                this.btnSave.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            try
            {
                var v_fdialog = new OpenFileDialog();
                v_fdialog.Filter = "Image Format (*.bmp, *.jpg, *.gif)|*.bmp;*.jpg;*.gif|All Files (*.*)|*.*";
                var v_dialogResult = v_fdialog.ShowDialog();
                if (v_dialogResult == DialogResult.OK)
                {
                    var v_fbuffer = FileReader.ReadAllBytes(v_fdialog.FileName);
                    this.profileImageBox.Image = Image.FromStream(new MemoryStream(v_fbuffer));
                    this.profileImageBox.Tag = v_fdialog.FileName;
                    this.label2.Text = Path.GetFileName(v_fdialog.FileName);
                    // enable nút lưu
                    this.btnSave.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private Image LoadImageProfile()
        {
            if (m_user_profile.HasProfile == true)
            {
                return Bitmap.FromStream(m_user_profile.GetPictureStream());
            }
            else
            {
                return Resources.NoImage;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                m_user_profile.UserName = txtUserName.Text;
                m_user_profile.SaveUserPicture(profileImageBox.Tag as string);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
